package com.jzg.jcptadmin.ui;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.MD5Utils;
import com.jzg.jcptadmin.Utils.ScreenUtils;
import com.jzg.jcptadmin.Utils.UIUtils;
import com.jzg.jcptadmin.adpter.VinListAdapter;
import com.jzg.jcptadmin.app.ActivityHelp;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.Table;
import com.jzg.jcptadmin.data.vo.ValuationDetailData;
import com.jzg.jcptadmin.data.vo.VinDetails;
import com.jzg.jcptadmin.presenter.VinResultPresenter;
import com.jzg.jcptadmin.viewinterface.VinResultInterface;

import org.honorato.multistatetogglebutton.MultiStateToggleButton;
import org.honorato.multistatetogglebutton.ToggleButton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangyd on 15/12/28.
 * VIN码查询结果
 */
public class VinResultActivity extends BaseActivity implements View.OnClickListener, VinResultInterface {
    @Bind(R.id.title_content)
    TextView title_content;
    @Bind(R.id.title_return)
    ImageView title_return;
    @Bind(R.id.title_history)
    ImageView title_history;

    @Bind(R.id.vin_text)
    TextView vin_text;

    @Bind(R.id.brand_name)
    TextView brand_name;

    @Bind(R.id.vin_list)
    ListView vin_list;

    @Bind(R.id.scrollView)
    ScrollView scrollView;

    @Bind(R.id.no_data_viewstub)
    ViewStub no_data_viewstub;

    @Bind(R.id.content_layout)
    LinearLayout content_layout;
    @Bind(R.id.mstb_multi_id)
    MultiStateToggleButton mstbMultiId;
    @Bind(R.id.mstb_multi_biansu)
    MultiStateToggleButton mstbMultiBiansu;
    @Bind(R.id.ll_biansuxiang)
    LinearLayout llBiansuxiang;
    @Bind(R.id.ll_paiqiliang)
    LinearLayout llPaiqiliang;

    private VinResultPresenter vinResultPresenter;

    private VinListAdapter vinListAdapter;

    //vin查询结果的汽车列表数据
    private List<Table> carItmes;

    //vin查询结果详情（包含这个界面显示用到的所有数据）
    private VinDetails vinDetails;

    //当前vin查询列表点击位置
    private int curVinPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vin_result);
        ButterKnife.bind(this);
        title_content.setText(getResources().getString(R.string.vin_result_title));
        vinDetails = getIntent().getParcelableExtra(ActivityHelp.VINDETAILS);

        //vin码有效的时候显示相关详情数据
        if (vinDetails.getStatus() == Constant.SUCCESS) {
            vinResultPresenter = new VinResultPresenter(DataManager.getInstance());
            vinResultPresenter.attachView(this);
            initView();
        }
        //vin码无效的时候显示占无数据界面
        else {
            int height = ScreenUtils.getScreenHeight(this);
            int statusHeight = ScreenUtils.getStatusHeight(this);
            int titleHeight = (int) getResources().getDimension(R.dimen.title_height);
//            Logger.i(height + ":" + statusHeight + ":" + titleHeight);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height - titleHeight - statusHeight);
            no_data_viewstub.setLayoutParams(params);
            no_data_viewstub.setVisibility(View.VISIBLE);
            content_layout.setVisibility(View.GONE);
        }
    }

    private void initView() {

        brand_name.setText(vinDetails.getCarName());
        vin_text.setText(vinDetails.getVin());

        //设置变速箱列表个数生成GirdView适配
        if (vinDetails != null && vinDetails.getBsxList() != null) {
            if (vinDetails.getBsxList().size()==0||"".equals(vinDetails.getBsxList())) {
                llBiansuxiang.setVisibility(View.GONE);
            } else {
                llBiansuxiang.setVisibility(View.VISIBLE);
            }
        }
        if (vinDetails != null && vinDetails.getBsxList() != null) {
            if (vinDetails.getPqlList().size()==0||"".equals(vinDetails.getPqlList())) {
                llPaiqiliang.setVisibility(View.GONE);
            } else {
                llPaiqiliang.setVisibility(View.VISIBLE);
            }
        }
        if(vinDetails!=null&&vinDetails.getBsxList()!=null&&vinDetails.getBsxList().size()>0){
            vinResultPresenter.setBsxList(vinDetails.getBsxList());
            mstbMultiId.setElements(vinDetails.getBsxList(), vinDetails.getBsxList().get(0));
            mstbMultiId.setColorRes(R.color.btn_blue, R.color.white);
            mstbMultiId.setOnValueChangedListener(new ToggleButton.OnValueChangedListener() {
                @Override
                public void onValueChanged(int value) {

                    //设置当前变速箱选择值
                    vinResultPresenter.setCurBsx(vinResultPresenter.getBsxList().get(value));
                    //根据当前变速箱和排量选项值进行列表过滤
                    vinResultPresenter.filterVinList();
                }
            });
        }
//        vinResultPresenter.setBsxList(vinDetails.getBsxList());
//        mstbMultiId.setElements(vinDetails.getBsxList(), vinDetails.getBsxList().get(0));
//        mstbMultiId.setColorRes(R.color.btn_blue, R.color.white);
//        mstbMultiId.setOnValueChangedListener(new ToggleButton.OnValueChangedListener() {
//            @Override
//            public void onValueChanged(int value) {
//                //设置当前变速箱选择值
//                vinResultPresenter.setCurBsx(vinResultPresenter.getBsxList().get(value));
//                //根据当前变速箱和排量选项值进行列表过滤
//                vinResultPresenter.filterVinList();
//            }
//        });

        if(vinDetails!=null&&vinDetails.getBsxList()!=null&&vinDetails.getPqlList().size()>0){
            //设置排量列表个数生成GirdView适配
            vinResultPresenter.setPlList(vinDetails.getPqlList());
            mstbMultiBiansu.setElements(vinDetails.getPqlList(), vinDetails.getPqlList().get(0));
            mstbMultiBiansu.setColorRes(R.color.btn_blue, R.color.white);
            mstbMultiBiansu.setOnValueChangedListener(new ToggleButton.OnValueChangedListener() {
                @Override
                public void onValueChanged(int value) {
                    //设置当前排气量选择值
                    vinResultPresenter.setCurPl(vinResultPresenter.getPlList().get(value));
                    //根据当前变速箱和排量选项值进行列表过滤
                    vinResultPresenter.filterVinList();
                }
            });
        }
//        //设置排量列表个数生成GirdView适配
//        vinResultPresenter.setPlList(vinDetails.getPqlList());
//
//        mstbMultiBiansu.setElements(vinDetails.getPqlList(), vinDetails.getPqlList().get(0));
//        mstbMultiBiansu.setColorRes(R.color.btn_blue, R.color.white);
//        mstbMultiBiansu.setOnValueChangedListener(new ToggleButton.OnValueChangedListener() {
//            @Override
//            public void onValueChanged(int value) {
//                //设置当前排气量选择值
//                vinResultPresenter.setCurPl(vinResultPresenter.getPlList().get(value));
//                //根据当前变速箱和排量选项值进行列表过滤
//                vinResultPresenter.filterVinList();
//            }
//        });

        vin_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                curVinPosition = position;
                vinResultPresenter.loadEstimate();
            }
        });

        //显示全部vin列表结果数据
        vinResultPresenter.setTable(vinDetails.getTable());
        showVinResult(vinDetails.getTable());
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    @OnClick({R.id.title_return, R.id.title_history})
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.title_return:
//                Logger.i("title_return");
                finish();
                break;
            case R.id.title_history:
                toHistoryActivity();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        if (vinResultPresenter != null)
            vinResultPresenter.detachView();
    }

    /**
     * 显示错误信息
     *
     * @param error 错误信息
     */
    @Override
    public void showError(String error) {
        Toast.makeText(this, Constant.errr_net, Toast.LENGTH_LONG).show();
    }

    /**
     * 显示结果信息
     *
     * @param carItmes
     */
    @Override
    public void showVinResult(List<Table> carItmes) {
        this.carItmes = carItmes;
        vinListAdapter = new VinListAdapter(this, carItmes);
        vin_list.setAdapter(vinListAdapter);
        UIUtils.setListViewHeight(vin_list);
        UIUtils.toScrollViewTop(scrollView);
    }

    /**
     * 通知vin结果列表改变
     *
     * @param filterTable
     */
    @Override
    public void notifyChange(List<Table> filterTable) {
        if (vinListAdapter == null) return;
        this.carItmes = filterTable;
        vinListAdapter.setData(filterTable);
        vinListAdapter.notifyDataSetChanged();
        UIUtils.setListViewHeight(vin_list);
        UIUtils.toScrollViewTop(scrollView);
    }

    @Override
    public Map<String, String> getEstimateParams() {
        Map<String, String> params = new HashMap<>();
        params.put("tokenid", "6");
        params.put("UserId", String.valueOf(appContext.getUser().getUserId()));
        params.put("styleId", carItmes.get(curVinPosition).getId());
        Map<String, Object> maps = new HashMap<>();
        maps.putAll(params);
        params.put("sign", MD5Utils.getMD5Sign(maps));
        return params;
    }

    @Override
    public void loadValuationDetail(ValuationDetailData valuationDetailData) {
        ActivityHelp.startActivity(this, ValuationDetailActivity.class, ActivityHelp.ESTIMATEDETAILS, valuationDetailData);

    }

    public void toHistoryActivity() {
        ActivityHelp.startActivity(this, HistoryActivity.class);
    }

}
